<?php
namespace Database;

class WorkList extends Base
{

    public function __construct()
    {
        $this->setModel(MODEL_WORKLIST);
    }

    public function getIndex($page, $limit, $extension = [])
    {
        return $this->getIndexByOrder($page, $limit, 'dry_data_sort', 'asc');
    }

    public function getParent()
    {
        $sql = $this->getSql();
        $sql->field('*');
        $sql->table($this->getTable());
        $sql->setOrder('dry_data_sort', 'asc');
        $list = $this->fetchAll($sql->get());
        return $list;
    }

    public function getList($userId, $page, $limit)
    {
        $sql = $this->getSql();
        $sql->field('*');
        $sql->table($this->getTable());
        $sql->where('dry_user', '=', $userId);
        $sql->setOrder('id', 'desc');
        $sql->setPage($page);
        $sql->setPageSize($limit);
        $sql->setLimit();
        $list = $this->fetchAll($sql->get());
        $count = $this->fetchColumn($sql->getTotalSql());
        return [
            'list' => $list,
            'count' => $count
        ];
    }

    public function getListByFields($data = [], $page, $limit)
    {
        $sql = $this->getSql();
        $sql->table($this->getTable());
        $sql->setPage($page);
        $sql->setPageSize($limit);
        $sql->setLimit();
        $sql->where('dry_user', '=', $data['dry_user']);
        unset($data['dry_user']);
        if($data['dateMin']){
            $data['date'][0] = $data['dateMin'];
            $data['date'][1] = $data['dateMax'];
            $sql->andWhere('dry_add_date', 'between and', $data['date']);
            unset($data['dateMin'], $data['date'], $data['dateMax']);
        }
        if($data['dry_project']){
            $sql->andWhere('dry_project', '=', $data['dry_project']);
            unset($data['dry_project']);
        }
        if($data['dry_worker']){
            $sql->andWhere('dry_worker', 'like', '%'.$data['dry_worker'].'%');
            unset($data['dry_worker']);
        }
        if(count($data) > 0){
            foreach($data as $field => $value){
                $sql->andWhere($field, '=', $value);
            }
        }
//        print_r($sql->get());
        $list = $this->fetchAll($sql->get());
        $count = $this->fetchColumn($sql->getTotalSql());
        return [
            'list' => $list,
            'count' => $count
        ];
    }
}